package com.atguigu.gmall.cart.service.impl;

import com.atguigu.gmall.cart.mapper.CartInfoMapper;
import com.atguigu.gmall.cart.service.CartInfoAsyncService;
import com.atguigu.gmall.model.cart.CartInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class CartInfoAsyncServiceImpl implements CartInfoAsyncService {
    @Autowired
    private CartInfoMapper cartInfoMapper;

    @Override
    @Async//开启子线程注解
    public void updateAsync(CartInfo cartInfo) {
        System.out.println("updateAsync:子线程名称:" + Thread.currentThread().getName());
        //cartInfoMapper.updateById(cartInfo);
        cartInfoMapper.update(cartInfo,new QueryWrapper<CartInfo>()
                .eq("user_id",cartInfo.getUserId())
                .eq("sku_id",cartInfo.getSkuId()));
    }

    @Override
    @Async//开启子线程注解
    public void insertAsync(CartInfo cartInfo) {
        System.out.println("insertAsync:子线程名称:" + Thread.currentThread().getName());
        cartInfoMapper.insert(cartInfo);
    }

    //批量的将购物车数据 临时用户 更新成真实用户的数据
    @Override
    @Async//开启子线程注解
    public void updateAsyncByUserId(String userTempId, String userId) {
        System.out.println("insertAsync:子线程名称:" + Thread.currentThread().getName());
        CartInfo cartInfo = new CartInfo();
        cartInfo.setUserId(userId);
        cartInfoMapper.update(cartInfo,new QueryWrapper<CartInfo>()
        .eq("user_id",userTempId));
    }

    //删除
    @Override
    @Async
    public void deleteByUserIdAndSkuId(CartInfo cartInfoByUserTempId) {
        cartInfoMapper.delete(new QueryWrapper<CartInfo>()
        .eq("sku_id",cartInfoByUserTempId.getSkuId())
        .eq("user_id",cartInfoByUserTempId.getUserId()));
    }

    //单独修改临时用户为真实用户id
    @Override
    //@Async//开启子线程注解
    public void updateAsyncByUserTempIdAndSkuId(CartInfo cartInfoByUserTempId, String userId) {
        CartInfo cartInfo = new CartInfo();
        cartInfo.setUserId(userId);
        cartInfoMapper.update(cartInfo,new QueryWrapper<CartInfo>()
        .eq("sku_id",cartInfoByUserTempId.getSkuId())
        .eq("user_id",cartInfoByUserTempId.getUserId()));
    }


}
